package com.yanqu.road.server.cross3server.cmd.activity.union;

import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ChannelTonBingProto;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.server.channelserver.manager.activity.tonbing.ChannelTonBingMgr;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.connect.CrossAgentLinkedControl;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.ArrayList;
import java.util.List;


@Cmd(code = Cross3Protocol.C3_ACTIVITY_GET_UNION_INFO, desc = "")
public class Cross3GetUnionReqCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ChannelTonBingProto.ChannelGetUnionReqMsg reqMsg = ChannelTonBingProto.ChannelGetUnionReqMsg.parseFrom(packet.getBytes());
        //来拉商会排行榜的
        if(reqMsg.getActivityId() != 0){
            int reqChannelId = reqMsg.getChannelId();
            int systemChannelId = ConfigHelper.getInt("channelId");
            if(reqChannelId != systemChannelId){
                ChannelTonBingMgr.getLogger().info("出现了不是这个渠道却发过来请求渠道的活动{},服务器渠道{},请求渠道{}",
                        reqMsg.getActivityId(),systemChannelId,reqChannelId);
                return;
            }
            List<Long> serverIdList = new ArrayList<>(reqMsg.getServerIdList());
            RandomHelper randomHelper = new RandomHelper();
            while (serverIdList.size() > 0){
                int index = randomHelper.next(0,serverIdList.size());
                long serverId = serverIdList.get(index);
                int serverChannelId = ServerListMgr.getChannelId(serverId);
//            if(CrossAgentLinkedControl.isSeverIdConnection(serverId)){
                if(serverChannelId == reqChannelId){
                    MessageHelper.sendPacket(serverId,0,YanQuMessageUtils.buildMessage(CrossProtocol.C3_CHANNEL_ACTIVITY_GET_UNION_INFO, reqMsg));
                    return;
                }else{
                    serverIdList.remove(serverId);
                }
            }
        }else{ //来拉渠道对应区服的
            List<ServerInfo> serverInfoList = ServerListMgr.getChannelServerList(reqMsg.getChannelId());
            List<Long> serverIdList = new ArrayList<>();
            for(ServerInfo serverInfo : serverInfoList){
                serverIdList.add(serverInfo.getServerId());
            }
            CrossUnionProto.ChannelGetUnionMaxAbilityRankListResp.Builder respMsg = CrossUnionProto.ChannelGetUnionMaxAbilityRankListResp.newBuilder();
            respMsg.setActivityId(0);
            respMsg.setChannel(reqMsg.getChannelId());
            respMsg.addAllServerId(serverIdList);
            MessageHelper.sendChannelServer(YanQuMessageUtils.buildMessage(ChannelProtocol.N_ACTIVITY_GET_UNION_INFO_RESP, respMsg));
        }
    }

    @Override
    public void sendData() {

    }

}
